const prefix = 'diamond-galaxy-admin-'
function set(key: string, val: any): void {
  try {
    window.localStorage.setItem(`${prefix}${key}`, JSON.stringify(val));
  } catch (err) {
    console.error(err);
  }
}

function get(key: string): any {
  const value = window.localStorage.getItem(`${prefix}${key}`);
  try {
    return JSON.parse(value as string);
  } catch (err) {
    console.error(err);
    return value;
  }
}

function remove(key: string): void {
  try {
    window.localStorage.removeItem(`${prefix}${key}`);
  } catch (err) {
    console.error(err);
  }
}

function clear(): void {
  try {
    window.localStorage.clear();
  } catch (err) {
    console.error(err);
  }
}

interface Local {
  set: (key: string, val: any) => void,
  get: (key: string) => any,
  remove: (key: string) => void,
  clear: () => void
}

const local: Local = {
  set,
  get,
  remove,
  clear,
};

export default local;
